/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.garagesale.user.web.form;

import com.garagesale.data.Item;
import com.garagesale.data.ShoppingCart;
import com.garagesale.data.User;
import com.garagesale.web.util.WebAction;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import static com.garagesale.web.util.FormUtils.ensureExistence;
import static com.garagesale.web.util.FormUtils.ensureNonZeroExistence;

/**
 *
 * @author christophe
 */
public class UserForm extends org.apache.struts.action.ActionForm
{

    private Integer userID;
    private String lastName;
    private String firstName;
    private String username;
    private String password;
    private String emailAddress;
    private Set<Item> items;
    private ShoppingCart shoppingCart;
    protected String action;

    /**
     * Get the value of action
     *
     * @return the value of action
     */
    public String getAction()
    {
        return action;
    }

    /**
     * Set the value of action
     *
     * @param action new value of action
     */
    public void setAction(String action)
    {
        this.action = action;
    }


    public String getEmailAddress()
    {
        return emailAddress;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public Set<Item> getItems()
    {
        return items;
    }

    public String getLastName()
    {
        return lastName;
    }

    public String getPassword()
    {
        return password;
    }

    public ShoppingCart getShoppingCart()
    {
        return shoppingCart;
    }

    public Integer getUserID()
    {
        return userID;
    }

    public String getUsername()
    {
        return username;
    }

    public void setEmailAddress(String emailAddress)
    {
        this.emailAddress = emailAddress;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public void setItems(Set<Item> items)
    {
        this.items = items;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public void setShoppingCart(ShoppingCart shoppingCart)
    {
        this.shoppingCart = shoppingCart;
    }

    public void setUserID(Integer userId)
    {
        this.userID = userId;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public void fromUser(User user)
    {
        this.userID = user.getUserID();
        this.emailAddress = user.getEmailAddress();
        this.firstName = user.getFirstName();
        this.lastName = user.getLastName();
        this.password = user.getPassword();
        this.username = user.getUsername();
    }


    public User toUser()
    {
        User user = new User();

        user.setUserID(userID);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        user.setEmailAddress(emailAddress);
        user.setUsername(username);
        user.setPassword(password);


        return user;
    }

    /**
     *
     */
    public UserForm()
    {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = new ActionErrors();

        if (WebAction.DELETE.isAction(action))
        {
            ensureNonZeroExistence(userID, "User ID", errors);
        }
        else
        {
            ensureExistence(lastName, "Last Name", errors);
            ensureExistence(firstName, "First Name", errors);
            ensureExistence(emailAddress, "Email Address", errors);
            ensureExistence(username, "User Name", errors);
            ensureExistence(password, "Password", errors);
        }

        return errors;
    }
}
